<template>
	<view class="container authorize">
		<view class="authorize-con">
        <view class="icon">
		    		<image class="icon"  src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/indexicon.jpg" mode=""></image>
        </view>
<!--			<view class="btn" @click="getUserInfo">-->
<!--        微信-->
<!--        授权-->
<!--			</view>-->
      <button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
    </view>

	</view>
</template>

<script>
	// import { routerJump } from '@/common/common.js'
	import { mapState } from 'vuex'
	export default {
		name: 'authorize',
		data() {
			return {
				menuheight: 0,
				loginRes: '',
				loginAuthRes: '',
				loginAuthErr: '',
			}
		},
		computed: {
			// ...mapState({
			// 	token: state => state.token,
			// 	theme: state => state.theme,
			// 	openId: state => state.openId,
			// 	userProfile: state => state.userProfile,
			// 	loginAuth: state => state.loginAuth,
			// })
		},
		onPullDownRefresh() {
			
		},
		onLoad() {
			this.InitData()
		},
		methods: {
			InitData() {
				// #ifdef MP-WEIXIN
				// let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
				// this.menuheight = menuButtonInfo.top + menuButtonInfo.height
				// // #endif
				// let title = this.$t('pages.authorize')
				// this.$bluetooth('themeInit')
				// this.$bluetooth('title', title)
			},
			// routerJump (path, query, pathType) {
			//   routerJump(path, query, pathType)
			// },


      getPhoneNumber(e){


        console.log(e.detail.code)  // 动态令牌
        console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
        console.log(e.detail.errno)
        console.log(this.$store.state.wxUserInfo.openId)
        console.log(this.$store.state.wxNickName)
        // let appid=this.$store.state.wxNickName.watermark.appid;
        this.$api.request({
          // url: this.$api.getPhone+"?appid="+this.$appId+"&code="+e.detail.code ,
          url: this.$api.getPhone,
          method:"POST",
          data:{

            "openid":this.$store.state.wxUserInfo.openId,
              "code":e.detail.code,
              "nickName":this.$store.state.wxNickName.nickName,
              "avatarUrl":this.$store.state.wxNickName.avatarUrl
          }

        }).then(result => {
          console.log(result)
          console.log(result.data.tokenValue)
          this.$store.state.token=result.data.tokenValue
          uni.setStorageSync("token",result.data.tokenValue)
          setTimeout(res=>{
            uni.navigateBack()
          },500)

        })
      },
			getUserInfo () {
				let that = this
				uni.showLoading({
					mask: true
				})
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						console.log(res.provider)
						if (~res.provider.indexOf('weixin')) {
							console.log('provider')
							uni.login({
								provider: res.provider,
								success: (loginRes) => {
									that.loginRes = loginRes
								},
								fail: (err) => {
									console.log(err)
								},
							})
							uni.getUserProfile({
								lang: 'zh_CN',
								desc: '登录',
								success: (userRes) => {
									console.log("user", userRes)
									// that.$store.dispatch('stateActions', {key: 'userProfile', data: userRes})
									let timer = setTimeout(() => {
										clearTimeout(timer)
										that.getLogin(that.loginRes)
									}, 500)
								},
								fail: (err) => {
									console.log(err)
									console.log('err')
									uni.showToast({
										title: '获取登录信息失败，请重新授权！',
										icon: 'none'
									})
								},
								complete: (arg) => {
									// that.getLogin(that.loginRes)
								}
							})
						}
					}
				})
			},
			getLogin () {
        // let url=this.$api.auth + '?appid=' + this.$appId + '&code=' + loginRes.code;
        // console.log(url)
        // console.log(this.$store.state.wxUserInfo)
        let data=this.$store.state.wxUserInfo;
        let that=this;
            wx.getUserInfo({
              success: function(res) {
                console.log(res)
                var userInfo = res.userInfo
                console.log(res.rawData)
                var nickName = userInfo.nickName
                var avatarUrl = userInfo.avatarUrl
                var gender = userInfo.gender //性别 0：未知、1：男、2：女
                var province = userInfo.province
                var city = userInfo.city
                var country = userInfo.country
                //解密信息
                that.$api.request({
                  url: that.$api.info ,
                    data:{"appid": that.$appId,"encryptedData":res.encryptedData,"iv":res.iv,
                    "signature":res.signature,"sessionKey":data.sessionKey,
                  }
                }).then(res=>{
                  that.$store.state.token=res.data.tokenValue
                  uni.setStorageSync("token",res.data.tokenValue)
                })

              }
            })


			},
		}
	}
</script>

<style lang="scss" scoped>
.authorize {
  padding: 30rpx 30rpx;
  flex-direction: column;
  justify-content: center;
  &-con {
    padding-top: 300rpx;
    width: 100%;
    text-align: center;
    .icon {
      width: 250rpx;
      height: 250rpx;
      margin: 0 auto;
    }
    .btn {
      width: 100%;
      margin-top: 100rpx;
      margin-bottom: 250rpx;
      overflow: hidden;
      height: 100rpx;
      line-height: 100rpx;
      text-align: center;
      background-color: #23C1A4;
      color: #fff;
      border-radius: 50rpx;
      font-size: 36rpx;
      font-family: "PingFangSC-Semibold";
      font-weight: 600;
      position: relative;
      button {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
        z-index: 9;
      }
    }
  }
}
</style>
